/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.config.controller;

import com.icinfo.cloud.common.core.Validator.BaseGroup;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterPageRequest;
import com.icinfo.cloud.provider.punish.config.dto.ConfigCaseApprovalCenterUseRequest;
import com.icinfo.cloud.provider.punish.config.model.ConfigCaseApprovalCenter;
import com.icinfo.cloud.provider.punish.config.service.IConfigCaseApprovalCenterService;
import com.icinfo.cloud.provider.punish.config.service.impl.ConfigBusiParamMainServiceImpl;
import com.icinfo.cloud.provider.punish.config.vo.ConfigCaseApprovalCenterVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:  config_case_approval_center 对应的Controller类.<br>
 *
 * @author framework generator
 * @date 2022年04月01日
 */
@RestController
@RequestMapping("/config/caseApprovalCenter")
public class ConfigCaseApprovalCenterController extends BaseController {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigCaseApprovalCenterController.class);

    @Autowired
    private IConfigCaseApprovalCenterService configCaseApprovalCenterService;
    @Resource
    private ConfigBusiParamMainServiceImpl configBusiParamMainService;

    /**
     * 【V1.1.0】描述：案审配置分页列表
     * @author liyafeng
     * @date 2022/4/1
     * @param request
     * @return {@link Result < PageResponse< ConfigCaseApprovalCenterVo>>}
     */
    @PostMapping("/list")
    public Result<PageResponse<ConfigCaseApprovalCenterVo>> list(@RequestBody @Validated ConfigCaseApprovalCenterPageRequest request) {
        List<ConfigCaseApprovalCenterVo> configCaseApprovalCenterVos=configCaseApprovalCenterService.getConfigCaseApprovalCenterVoList(request);
        return Result.success("查询成功",new PageResponse<>(configCaseApprovalCenterVos));
    }

    /**
     *【V1.1.0】描述：保存案审配置
     * @author liyafeng
     * @date 2022/4/1
     * @param configCaseApprovalCenterDto
     * @return {@link Result< String>}
     */
    @PostMapping("/save")
    public Result<String>  save(@RequestBody ConfigCaseApprovalCenterDto configCaseApprovalCenterDto) {
        //适用区域类型；0 全省，1指定类别
        String applyAreaType = configCaseApprovalCenterDto.getApplyAreaType();
        List<String> applyAreaCodeList = configCaseApprovalCenterDto.getApplyAreaCodeList();
        if(Constant.STRING_0.equals(applyAreaType)){
            if(!ObjectUtils.isEmpty(applyAreaCodeList) && applyAreaCodeList.size() > 0){
                return Result.error("使用区域类型未全省时，请将适用区域置空");
            }
        } else if(Constant.STRING_1.equals(applyAreaType)){
            if(ObjectUtils.isEmpty(applyAreaCodeList) || applyAreaCodeList.size() == 0){
                return Result.error("未指定适用区域编码信息");
            }
            List<String> applyAreaNameList = configCaseApprovalCenterDto.getApplyAreaNameList();
            if(ObjectUtils.isEmpty(applyAreaNameList) || applyAreaNameList.size() == 0){
                return Result.error("未指定适用区域名称信息");
            }
            //处理区域编号、名称
            configCaseApprovalCenterDto.setApplyAreaCode(configBusiParamMainService.getFormData(applyAreaCodeList));
            configCaseApprovalCenterDto.setApplyAreaName(configBusiParamMainService.getFormData(applyAreaNameList));
        } else {
            return Result.error("非法适用区域类型（0 全省，1指定类别）");
        }
        boolean isExist=configCaseApprovalCenterService.checkIsExist(configCaseApprovalCenterDto);
        if(isExist){
            return Result.error("已存在相同范围的配置项，请检查！");
        }
        int num=configCaseApprovalCenterService.saveConfigCaseApprovalCenterDto(configCaseApprovalCenterDto);
        if(num>0){
            return Result.success("保存成功");
        }else {
            return Result.error("保存失败");
        }
    }

    /**
     *【V1.1.0】描述：修改案审配置
     * @author liyafeng
     * @date 2022/4/1
     * @param configCaseApprovalCenterDto
     * @return {@link Result< String>}
     */
    @PostMapping("/edit")
    public Result<String>  edit(@RequestBody @Validated(BaseGroup.Update.class) ConfigCaseApprovalCenterDto configCaseApprovalCenterDto) {
        //适用区域类型；0 全省，1指定类别
        String applyAreaType = configCaseApprovalCenterDto.getApplyAreaType();
        List<String> applyAreaCodeList = configCaseApprovalCenterDto.getApplyAreaCodeList();
        if(Constant.STRING_0.equals(applyAreaType)){
            if(!ObjectUtils.isEmpty(applyAreaCodeList) && applyAreaCodeList.size() > 0){
                return Result.error("使用区域类型未全省时，请将适用区域置空");
            }
        } else if(Constant.STRING_1.equals(applyAreaType)){
            if(ObjectUtils.isEmpty(applyAreaCodeList) || applyAreaCodeList.size() == 0){
                return Result.error("未指定适用区域编码信息");
            }
            List<String> applyAreaNameList = configCaseApprovalCenterDto.getApplyAreaNameList();
            if(ObjectUtils.isEmpty(applyAreaNameList) || applyAreaNameList.size() == 0){
                return Result.error("未指定适用区域名称信息");
            }
            //处理区域编号、名称
            configCaseApprovalCenterDto.setApplyAreaCode(configBusiParamMainService.getFormData(applyAreaCodeList));
            configCaseApprovalCenterDto.setApplyAreaName(configBusiParamMainService.getFormData(applyAreaNameList));
        } else {
            return Result.error("非法适用区域类型（0 全省，1指定类别）");
        }
        int num=configCaseApprovalCenterService.editConfigCaseApprovalCenter(configCaseApprovalCenterDto);
        if(num>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    /**
     *【V1.1.0】描述：案审配置启用或禁用
     * @author liyafeng
     * @date 2022/4/1
     * @param param
     * @return {@link Result< String>}
     */
    @PostMapping("/edituse")
    public Result<String>  use(@RequestBody @Validated(BaseGroup.Update.class) ConfigCaseApprovalCenterUseRequest param) {
        int num=configCaseApprovalCenterService.editUsed(param);
        if(num>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    /**
     * 【V1.1.0】描述：获取案审配置详情
     * @author liyafeng
     * @date 2022/4/1
     * @param id
     * @return {@link Result}
     */
    @PostMapping("/getDetail/{id}")
    public Result<ConfigCaseApprovalCenter> detail(@PathVariable("id") String id){
        ConfigCaseApprovalCenter configCaseApprovalCenter=configCaseApprovalCenterService.getDetail(id);
        return Result.success(configCaseApprovalCenter);
    }
}